#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <linux/types.h>          /* for videodev2.h */
#include <linux/videodev2.h>

int main(int argc, char const *argv[])
{
    int fd = 0;
    struct v4l2_fmtdesc fmtdesc;  // 用于描述视频格式的结构体
    struct v4l2_frmsizeenum fsenum;  // 用于枚举视频帧大小的结构体
    int fmt_index = 0;
    int fsenum_index = 0;

    /* 打开视频设备 */
    fd = open(argv[1], O_RDWR);
    if (fd < 0) {
        printf("open %s failed, errno: %d\n", argv[1], errno);
        return -1;
    }

    while (1) {
        /* 枚举视频格式 */
        fmtdesc.index = fmt_index;
        fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        if (ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) < 0) {
            break;
        }

        /* 枚举视频帧大小 */
        for (fsenum_index = 0; ; fsenum_index++) {

            memset(&fsenum, 0, sizeof(fsenum));
            fsenum.index = fsenum_index;
            fsenum.pixel_format = fmtdesc.pixelformat;
            fsenum.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
           
            if (ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &fsenum) < 0) {
                break;
            } else {
                printf("format %s,%d, framesize %d: %d x %d\n", fmtdesc.description, fmtdesc.pixelformat,\
                    fsenum_index, fsenum.discrete.width, fsenum.discrete.height);
            }
        }
        fmt_index++;
    }

    /* code */
    return 0;
}
